package shiyl.use.springboot.loadbalance;

import shiyl.use.springboot.loadbalance.impl.RandomPolicy;
import shiyl.use.springboot.loadbalance.impl.RoundRobinPolicy;

import java.util.HashMap;
import java.util.Map;

/**
 * 负载均衡策略工厂，管理所有负载均衡策略实现
 * @author shiyl
 */
public class LoadBalancePolicyFactory {

    private static Map<POLICY, ILoadBalancePolicy> policies = new HashMap<>();

    private LoadBalancePolicyFactory(){}

    static {
        policies.put(POLICY.RANDOM, new RandomPolicy());
        policies.put(POLICY.ROUNDROBIN, new RoundRobinPolicy());
    }

    public static ILoadBalancePolicy getPolicyByName( POLICY policy) {

        if( null == policy || policy.equals("") ){
            throw new RuntimeException("必须指定策略名称！");
        }

        if( policies.containsKey(policy)){
            return policies.get(policy);
        }
        throw new RuntimeException("找不到名称为["+policy+"]的负载均衡策略！");
    }
}
